
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>函数 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="pipe-operator.html" />
    
    
    <link rel="prev" href="control-structures.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="basics.html">
            
                <a href="basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="collections.html">
            
                <a href="collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="enum.html">
            
                <a href="enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="pattern-matching.html">
            
                <a href="pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="control-structures.html">
            
                <a href="control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.7" data-path="functions.html">
            
                <a href="functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="pipe-operator.html">
            
                <a href="pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="modules.html">
            
                <a href="modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="mix.html">
            
                <a href="mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="sigils.html">
            
                <a href="sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="documentation.html">
            
                <a href="documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="testing.html">
            
                <a href="testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="comprehensions.html">
            
                <a href="comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="strings.html">
            
                <a href="strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="date-time.html">
            
                <a href="date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="mix-tasks.html">
            
                <a href="mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="iex-helpers.html">
            
                <a href="iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../advanced/erlang.html">
            
                <a href="../advanced/erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../advanced/error-handling.html">
            
                <a href="../advanced/error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../advanced/escripts.html">
            
                <a href="../advanced/escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../advanced/concurrency.html">
            
                <a href="../advanced/concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../advanced/otp-concurrency.html">
            
                <a href="../advanced/otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../advanced/otp-supervisors.html">
            
                <a href="../advanced/otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../advanced/otp-distribution.html">
            
                <a href="../advanced/otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../advanced/metaprogramming.html">
            
                <a href="../advanced/metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="../advanced/umbrella-projects.html">
            
                <a href="../advanced/umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="../advanced/typespec.html">
            
                <a href="../advanced/typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="../advanced/behaviours.html">
            
                <a href="../advanced/behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="../advanced/gen-stage.html">
            
                <a href="../advanced/gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="../advanced/protocols.html">
            
                <a href="../advanced/protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="../advanced/nerves.html">
            
                <a href="../advanced/nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../ecto/basics.html">
            
                <a href="../ecto/basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../ecto/changesets.html">
            
                <a href="../ecto/changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../ecto/associations.html">
            
                <a href="../ecto/associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../ecto/querying.html">
            
                <a href="../ecto/querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../specifics/plug.html">
            
                <a href="../specifics/plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../specifics/eex.html">
            
                <a href="../specifics/eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../specifics/ets.html">
            
                <a href="../specifics/ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../specifics/mnesia.html">
            
                <a href="../specifics/mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../specifics/debugging.html">
            
                <a href="../specifics/debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../libraries/guardian.html">
            
                <a href="../libraries/guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../libraries/poolboy.html">
            
                <a href="../libraries/poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../libraries/benchee.html">
            
                <a href="../libraries/benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../libraries/bypass.html">
            
                <a href="../libraries/bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../libraries/distillery.html">
            
                <a href="../libraries/distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../libraries/stream-data.html">
            
                <a href="../libraries/stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >函数</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>Elixir &#x548C;&#x5176;&#x4ED6;&#x51FD;&#x6570;&#x5F0F;&#x8BED;&#x8A00;&#x4E00;&#x6837;&#xFF0C;&#x51FD;&#x6570;&#x90FD;&#x662F;&#x4E00;&#x7B49;&#x516C;&#x6C11;&#x3002;&#x6211;&#x4EEC;&#x5C06;&#x5B66;&#x4E60; Elixir &#x4E2D;&#x4E0D;&#x540C;&#x7C7B;&#x578B;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x5B83;&#x4EEC;&#x4E0E;&#x4F17;&#x4E0D;&#x540C;&#x7684;&#x5730;&#x65B9;&#xFF0C;&#x4EE5;&#x53CA;&#x5982;&#x4F55;&#x4F7F;&#x7528;&#x5B83;&#x4EEC;&#x3002;</p>
<h2 id="&#x533F;&#x540D;&#x51FD;&#x6570;">&#x533F;&#x540D;&#x51FD;&#x6570;</h2>
<p>&#x5C31;&#x50CF;&#x540D;&#x5B57;&#x4E2D;&#x8BF4;&#x660E;&#x7684;&#x90A3;&#x6837;&#xFF0C;&#x533F;&#x540D;&#x51FD;&#x6570;&#x6CA1;&#x6709;&#x540D;&#x5B57;&#x3002;&#x6211;&#x4EEC;&#x5728; <code>Enum</code> &#x8BFE;&#x7A0B;&#x4E2D;&#x770B;&#x5230;&#x8FC7;&#xFF0C;&#x5B83;&#x4EEC;&#x7ECF;&#x5E38;&#x88AB;&#x7528;&#x6765;&#x4F20;&#x9012;&#x7ED9;&#x5176;&#x4ED6;&#x51FD;&#x6570;&#x3002;
&#x8981;&#x5B9A;&#x4E49;&#x533F;&#x540D;&#x51FD;&#x6570;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981; <code>fn</code> &#x548C; <code>end</code> &#x5173;&#x952E;&#x5B57;&#xFF0C;&#x5728;&#x8FD9;&#x4E24;&#x8005;&#x4E4B;&#x95F4;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x4EFB;&#x610F;&#x6570;&#x91CF;&#x7684;&#x53C2;&#x6570;&#x548C;&#x51FD;&#x6570;&#x4F53;&#xFF0C;&#x5B83;&#x4EEC;&#x7528; <code>-&gt;</code> &#x5206;&#x9694;&#x5F00;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x6765;&#x770B;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; sum = <span class="hljs-keyword">fn</span> (a, b) -&gt; a + b <span class="hljs-keyword">end</span>
iex&gt; sum.(<span class="hljs-number">2</span>, <span class="hljs-number">3</span>)
<span class="hljs-number">5</span>
</code></pre>
<h3 id="&#x64CD;&#x4F5C;&#x7B26;">&amp; &#x64CD;&#x4F5C;&#x7B26;</h3>
<p>&#x56E0;&#x4E3A;&#x5728; Elixir &#x4E2D;&#x4F7F;&#x7528;&#x533F;&#x540D;&#x51FD;&#x6570;&#x975E;&#x5E38;&#x5E38;&#x89C1;&#xFF0C;&#x6240;&#x4EE5;&#x6709;&#x4E00;&#x4E2A;&#x5FEB;&#x6377;&#x65B9;&#x5F0F;&#x6765;&#x505A;&#x8FD9;&#x4EF6;&#x4E8B;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; sum = &amp;(&amp;<span class="hljs-number">1</span> + &amp;<span class="hljs-number">2</span>)
iex&gt; sum.(<span class="hljs-number">2</span>, <span class="hljs-number">3</span>)
<span class="hljs-number">5</span>
</code></pre>
<p>&#x4F60;&#x53EF;&#x80FD;&#x4E5F;&#x731C;&#x5230;&#x4E86;&#xFF0C;&#x5728;&#x8FD9;&#x79CD;&#x7B80;&#x5199;&#x7684;&#x6A21;&#x5F0F;&#x4E0B;&#xFF0C;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; <code>&amp;1</code>&#xFF0C;<code>&amp;2</code>&#xFF0C;<code>&amp;3</code> &#x7B49;&#x6765;&#x83B7;&#x53D6;&#x3002;</p>
<h2 id="&#x6A21;&#x5F0F;&#x5339;&#x914D;">&#x6A21;&#x5F0F;&#x5339;&#x914D;</h2>
<p>&#x5728; Elixir &#x4E2D;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#x4E0D;&#x4EC5;&#x9650;&#x4E8E;&#x53D8;&#x91CF;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x5728;&#x51FD;&#x6570;&#x7B7E;&#x540D;&#x4E0A;&#xFF0C;&#x6211;&#x4EEC;&#x5728;&#x540E;&#x9762;&#x7AE0;&#x8282;&#x4F1A;&#x770B;&#x5230;&#x8FD9;&#x4E2A;&#x529F;&#x80FD;&#x3002;
Elixir &#x4F7F;&#x7528;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#x6765;&#x627E;&#x5230;&#x7B2C;&#x4E00;&#x4E2A;&#x5339;&#x914D;&#x53C2;&#x6570;&#x7684;&#x6A21;&#x5F0F;&#xFF0C;&#x7136;&#x540E;&#x6267;&#x884C;&#x5B83;&#x540E;&#x9762;&#x7684;&#x51FD;&#x6570;&#x4F53;&#x3002;</p>
<pre><code class="lang-elixir">iex&gt; handle_result = <span class="hljs-keyword">fn</span>
...&gt;   {<span class="hljs-symbol">:ok</span>, result} -&gt; IO.puts <span class="hljs-string">&quot;Handling result...&quot;</span>
...&gt;   {<span class="hljs-symbol">:ok</span>, <span class="hljs-number">_</span>} -&gt; IO.puts <span class="hljs-string">&quot;This would be never run as previous will be matched beforehand.&quot;</span>
...&gt;   {<span class="hljs-symbol">:error</span>} -&gt; IO.puts <span class="hljs-string">&quot;An error has occurred!&quot;</span>
...&gt; <span class="hljs-keyword">end</span>

iex&gt; some_result = <span class="hljs-number">1</span>
iex&gt; handle_result.({<span class="hljs-symbol">:ok</span>, some_result})
Handling result...

iex&gt; handle_result.({<span class="hljs-symbol">:error</span>})
An error has occurred!
</code></pre>
<h2 id="&#x547D;&#x540D;&#x51FD;&#x6570;">&#x547D;&#x540D;&#x51FD;&#x6570;</h2>
<p>&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x6709;&#x540D;&#x5B57;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x8FD9;&#x6837;&#x5728;&#x540E;&#x9762;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x7528;&#x540D;&#x5B57;&#x6765;&#x4F7F;&#x7528;&#x5B83;&#x3002;&#x547D;&#x540D;&#x51FD;&#x6570;&#x901A;&#x8FC7; <code>def</code> &#x5173;&#x952E;&#x5B57;&#x5B9A;&#x4E49;&#x5728;&#x67D0;&#x4E2A;&#x6A21;&#x5757;&#x4E2D;&#xFF0C;&#x5173;&#x4E8E;&#x6A21;&#x5757;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x5728;&#x540E;&#x9762;&#x7684;&#x8BFE;&#x7A0B;&#x4E2D;&#x8BE6;&#x7EC6;&#x5B66;&#x4E60;&#xFF0C;
&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x53EA;&#x5173;&#x5FC3;&#x547D;&#x540D;&#x51FD;&#x6570;&#x3002;</p>
<p>&#x5B9A;&#x4E49;&#x5728;&#x6A21;&#x5757;&#x5185;&#x90E8;&#x7684;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x88AB;&#x5176;&#x4ED6;&#x6A21;&#x5757;&#x4F7F;&#x7528;&#xFF0C;&#x8FD9;&#x5728; Elixir &#x4E2D;&#x6784;&#x5EFA;&#x4EE3;&#x7801;&#x5757;&#x975E;&#x5E38;&#x6709;&#x7528;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Greeter</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello</span></span>(name) <span class="hljs-keyword">do</span>
    <span class="hljs-string">&quot;Hello, &quot;</span> &lt;&gt; name
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>

iex&gt; Greeter.hello(<span class="hljs-string">&quot;Sean&quot;</span>)
<span class="hljs-string">&quot;Hello, Sean&quot;</span>
</code></pre>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x7684;&#x51FD;&#x6570;&#x4F53;&#x53EA;&#x6709;&#x4E00;&#x884C;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7F29;&#x5199;&#x6210; <code>do:</code>&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Greeter</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello</span></span>(name), <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;Hello, &quot;</span> &lt;&gt; name
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x5B66;&#x5230;&#x4E86;&#x90A3;&#x4E48;&#x591A;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#x7684;&#x77E5;&#x8BC6;&#xFF0C;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x7528;&#x547D;&#x540D;&#x51FD;&#x6570;&#x5B9E;&#x73B0;&#x9012;&#x5F52;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Length</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">of</span></span>([]), <span class="hljs-symbol">do:</span> <span class="hljs-number">0</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">of</span></span>([<span class="hljs-number">_</span> | tail]), <span class="hljs-symbol">do:</span> <span class="hljs-number">1</span> + of(tail)
<span class="hljs-keyword">end</span>

iex&gt; Length.of []
<span class="hljs-number">0</span>
iex&gt; Length.of [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
<span class="hljs-number">3</span>
</code></pre>
<h3 id="&#x51FD;&#x6570;&#x540D;&#x5B57;&#x548C;&#x5143;&#x6570;">&#x51FD;&#x6570;&#x540D;&#x5B57;&#x548C;&#x5143;&#x6570;</h3>
<p>&#x6211;&#x4EEC;&#x4E4B;&#x524D;&#x63D0;&#x5230;&#x8FC7;&#xFF0C;&#x51FD;&#x6570;&#x540D;&#x79F0;&#x65B9;&#x5F0F;&#x7531;&#x540D;&#x5B57;&#x548C;&#x5143;&#x6570;&#x7EC4;&#x6210;&#xFF0C;&#x8FD9;&#x4E5F;&#x8868;&#x660E;&#x4F60;&#x53EF;&#x4EE5;&#x8FD9;&#x4E48;&#x505A;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Greeter2</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello</span></span>(), <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;Hello, anonymous person!&quot;</span>   <span class="hljs-comment"># hello/0</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello</span></span>(name), <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;Hello, &quot;</span> &lt;&gt; name        <span class="hljs-comment"># hello/1</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello</span></span>(name1, name2), <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;Hello, <span class="hljs-subst">#{name1}</span> and <span class="hljs-subst">#{name2}</span>&quot;</span>
                                                <span class="hljs-comment"># hello/2</span>
<span class="hljs-keyword">end</span>

iex&gt; Greeter2.hello()
<span class="hljs-string">&quot;Hello, anonymous person!&quot;</span>
iex&gt; Greeter2.hello(<span class="hljs-string">&quot;Fred&quot;</span>)
<span class="hljs-string">&quot;Hello, Fred&quot;</span>
iex&gt; Greeter2.hello(<span class="hljs-string">&quot;Fred&quot;</span>, <span class="hljs-string">&quot;Jane&quot;</span>)
<span class="hljs-string">&quot;Hello, Fred and Jane&quot;</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x5728;&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x6CE8;&#x91CA;&#x4E2D;&#x5217;&#x51FA;&#x4E86;&#x51FD;&#x6570;&#x7684;&#x5168;&#x79F0;&#x3002;&#x7B2C;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x4E0D;&#x63A5;&#x53D7;&#x4EFB;&#x4F55;&#x53C2;&#x6570;&#xFF0C;&#x56E0;&#x6B64;&#x662F; <code>hello/0</code>&#xFF1B;&#x7B2C;&#x4E8C;&#x4E2A;&#x51FD;&#x6570;&#x63A5;&#x53D7;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x56E0;&#x6B64;&#x662F; <code>hello/1</code>&#xFF0C;&#x4EE5;&#x6B64;&#x7C7B;&#x63A8;&#x3002;&#x4E0D;&#x540C;&#x4E8E;&#x5176;&#x4ED6;&#x8BED;&#x8A00;&#x7684;&#x51FD;&#x6570;&#x91CD;&#x8F7D;&#xFF0C;&#x8FD9;&#x4E9B;&#x51FD;&#x6570;&#x88AB;&#x8BA4;&#x4E3A;&#x662F;&#x4E0D;&#x540C;&#x7684;&#x3002;&#xFF08;&#x521A;&#x521A;&#x63D0;&#x5230;&#x8FC7;&#x7684;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#xFF0C;&#x53EA;&#x6709;&#x5F53;&#x51FD;&#x6570;&#x540D;&#x5B57;&#x548C;&#x63A5;&#x53D7;&#x7684;&#x53C2;&#x6570;&#x4E2A;&#x6570;&#x90FD;&#x5339;&#x914D;&#x7684;&#x65F6;&#x5019;&#x624D;&#x6210;&#x7ACB;&#x3002;&#xFF09;</p>
<h3 id="&#x51FD;&#x6570;&#x548C;&#x6A21;&#x5F0F;&#x5339;&#x914D;">&#x51FD;&#x6570;&#x548C;&#x6A21;&#x5F0F;&#x5339;&#x914D;</h3>
<p>&#x51FD;&#x6570;&#x8C03;&#x7528;&#x7684;&#x80CC;&#x540E;&#xFF0C;&#x5176;&#x5B9E;&#x4F7F;&#x7528;&#x4E86;&#x53EB;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#x7684;&#x65B9;&#x5F0F;&#x6765;&#x5904;&#x7406;&#x4F20;&#x5165;&#x7684;&#x53C2;&#x6570;&#x3002;</p>
<p>&#x6BD4;&#x5982;&#x8BF4;&#xFF0C;&#x6211;&#x4EEC;&#x7684;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x63A5;&#x6536;&#x4E00;&#x4E2A; map &#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x5BF9;&#x5176;&#x4E2D;&#x7684;&#x67D0;&#x4E00;&#x4E2A;&#x952E;&#x503C;&#x611F;&#x5174;&#x8DA3;&#x3002;&#x90A3;&#x4E48;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x6765;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x65F6;&#x9700;&#x8981;&#x7684;&#x952E;&#x503C;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Greeter1</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello</span></span>(%{<span class="hljs-symbol">name:</span> person_name}) <span class="hljs-keyword">do</span>
    IO.puts <span class="hljs-string">&quot;Hello, &quot;</span> &lt;&gt; person_name
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x5047;&#x8BBE;&#x6211;&#x4EEC;&#x73B0;&#x5728;&#x6709;&#x4E00;&#x4E2A;&#x5B57;&#x5178;&#xFF0C;&#x91CC;&#x9762;&#x5305;&#x542B;&#x4E86;&#x4E00;&#x4E2A;&#x4EBA;&#x7684;&#x540D;&#x5B57;&#xFF0C;Fred&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; fred = %{
...&gt; <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Fred&quot;</span>,
...&gt; <span class="hljs-symbol">age:</span> <span class="hljs-string">&quot;95&quot;</span>,
...&gt; <span class="hljs-symbol">favorite_color:</span> <span class="hljs-string">&quot;Taupe&quot;</span>  
...&gt; }
</code></pre>
<p>&#x4E0B;&#x9762;&#x5C31;&#x662F;&#x6211;&#x4EEC;&#x8C03;&#x7528; <code>Greeter1.hello/1</code> &#x5E76;&#x4F20;&#x5165;&#x5B57;&#x5178; <code>fred</code> &#x540E;&#x5F97;&#x5230;&#x7684;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-comment"># &#x4F20;&#x5165;&#x6574;&#x4E2A;&#x5B57;&#x5178;</span>
...&gt; Greeter1.hello(fred)
<span class="hljs-string">&quot;Hello, Fred&quot;</span>
</code></pre>
<p>&#x90A3;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5B57;&#x5178;&#x91CC;&#x9762; <em>&#x6CA1;&#x6709;</em> &#x5305;&#x542B; <code>:name</code> &#x8FD9;&#x4E2A;&#x952E;&#x5462;&#xFF1F;</p>
<pre><code class="lang-elixir"><span class="hljs-comment"># &#x4F20;&#x5165;&#x4E00;&#x4E2A;&#x4E0D;&#x5305;&#x542B;&#x76F8;&#x5E94;&#x952E;&#x503C;&#x7684;&#x5B57;&#x5178;&#x540E;&#xFF0C;&#x4F1A;&#x4EA7;&#x751F;&#x5982;&#x4E0B;&#x9519;&#x8BEF;</span>
...&gt; Greeter1.hello(%{<span class="hljs-symbol">age:</span> <span class="hljs-string">&quot;95&quot;</span>, <span class="hljs-symbol">favorite_color:</span> <span class="hljs-string">&quot;Taupe&quot;</span>})
** (FunctionClauseError) no function clause matching <span class="hljs-keyword">in</span> Greeter3.hello/<span class="hljs-number">1</span>    

    The following arguments were given to Greeter3.hello/<span class="hljs-number">1</span><span class="hljs-symbol">:</span>

        <span class="hljs-comment"># 1</span>
        %{<span class="hljs-symbol">age:</span> <span class="hljs-string">&quot;95&quot;</span>, <span class="hljs-symbol">favorite_color:</span> <span class="hljs-string">&quot;Taupe&quot;</span>}

    <span class="hljs-symbol">iex:</span><span class="hljs-number">12</span><span class="hljs-symbol">:</span> Greeter3.hello/<span class="hljs-number">1</span>
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x8868;&#x73B0;&#x884C;&#x4E3A;&#xFF0C;&#x662F;&#x7531;&#x4E8E; Elixir &#x5728;&#x5904;&#x7406;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x9700;&#x8981;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#x76F8;&#x5E94;&#x7684;&#x53C2;&#x6570;&#x3002;</p>
<p>&#x5F53; <code>Greeter1.hello/1</code> &#x88AB;&#x8C03;&#x7528;&#x65F6;&#xFF0C;&#x5B83;&#x7684;&#x6570;&#x636E;&#x5E94;&#x8BE5;&#x662F;&#x8FD9;&#x6837;&#x7684;&#xFF1A;</p>
<pre><code class="lang-Elixir"><span class="hljs-comment"># &#x4F20;&#x5165;&#x7684;&#x5B57;&#x5178;</span>
iex&gt; fred = %{
...&gt; <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Fred&quot;</span>,
...&gt; <span class="hljs-symbol">age:</span> <span class="hljs-string">&quot;95&quot;</span>,
...&gt; <span class="hljs-symbol">favorite_color:</span> <span class="hljs-string">&quot;Taupe&quot;</span>  
...&gt; }
</code></pre>
<p><code>Greeter1.hello/1</code> &#x671F;&#x671B;&#x7684;&#x53C2;&#x6570;&#x5219;&#x662F;&#xFF1A;</p>
<pre><code class="lang-elixir">%{<span class="hljs-symbol">name:</span> person_name}
</code></pre>
<p>&#x5728;&#x51FD;&#x6570; <code>Greeter1.hello/1</code> &#x91CC;, &#x6211;&#x4EEC;&#x4F20;&#x5165;&#x7684;&#x5B57;&#x5178;&#xFF08;<code>fred</code>&#xFF09;&#x5C31;&#x4F1A;&#x548C;&#x5B9A;&#x4E49;&#x7684;&#x53C2;&#x6570;&#xFF08;<code>%{name: person_name}</code>&#xFF09;&#x8FDB;&#x884C;&#x5339;&#x914D;&#xFF1A;</p>
<pre><code class="lang-elixir">%{<span class="hljs-symbol">name:</span> person_name} = %{<span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Fred&quot;</span>, <span class="hljs-symbol">age:</span> <span class="hljs-string">&quot;95&quot;</span>, <span class="hljs-symbol">favorite_color:</span> <span class="hljs-string">&quot;Taupe&quot;</span>}
</code></pre>
<p>&#x5F53;&#x5B83;&#x5728;&#x4F20;&#x5165;&#x7684;&#x5B57;&#x5178;&#x4E2D;&#x627E;&#x5230;&#x76F8;&#x5E94;&#x7684;&#x952E; <code>:name</code> &#x65F6;&#xFF0C;&#x8FD9;&#x4E2A;&#x5339;&#x914D;&#x5C31;&#x6210;&#x7ACB;&#x4E86;&#x3002;&#x5339;&#x914D;&#x6210;&#x529F;&#x7684;&#x7ED3;&#x679C;&#x5C31;&#x662F;&#xFF0C;&#x53F3;&#x8FB9;&#x5B57;&#x5178;&#x4E2D; <code>:name</code> &#x952E;&#x5BF9;&#x5E94;&#x7684;&#x503C;&#xFF08;&#x5728; <code>fred</code> &#x5B57;&#x5178;&#x4E2D;&#xFF09;&#xFF0C;&#x5C31;&#x8D4B;&#x4E88;&#x5230;&#x5DE6;&#x8FB9;&#x7684;&#x53D8;&#x91CF;&#xFF08;<code>person_name</code>&#xFF09;&#x4E0A;&#x3002;</p>
<p>&#x90A3;&#x4E48;&#xFF0C;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x9664;&#x4E86;&#x5E0C;&#x671B;&#x628A; Fred &#x7684;&#x59D3;&#x540D;&#x8D4B;&#x503C;&#x5230; <code>person_name</code> &#x5916;&#xFF0C;&#x6211;&#x4EEC;&#x8FD8;&#x5E0C;&#x671B;&#x4FDD;&#x7559;&#x6574;&#x4E2A;&#x4EBA;&#x7269;&#x4FE1;&#x606F;&#x7684;&#x5B57;&#x5178;&#xFF0C;&#x8981;&#x600E;&#x4E48;&#x505A;&#x5462;&#xFF1F;&#x6BD4;&#x5982;&#x8BF4;&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x5728;&#x548C;&#x4ED6;&#x6253;&#x62DB;&#x547C;&#x540E;&#x8FD0;&#x884C; <code>IO.inspect(fred)</code>&#x3002;&#x4E0A;&#x9762;&#x7684;&#x505A;&#x6CD5;&#xFF0C;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x53EA;&#x662F;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#x4E86; <code>:name</code> &#x8FD9;&#x4E2A;&#x952E;&#x503C;&#xFF0C;&#x4E5F;&#x56E0;&#x6B64;&#x53EA;&#x662F;&#x628A;&#x5B83;&#x7684;&#x503C;&#x8D4B;&#x4E88;&#x4E86;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#xFF0C;&#x51FD;&#x6570;&#x672C;&#x8EAB;&#x5E76;&#x4E0D;&#x77E5;&#x9053; Fred &#x5176;&#x5B83;&#x7684;&#x4FE1;&#x606F;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x4FDD;&#x5B58;&#x6240;&#x6709;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x628A;&#x6574;&#x4E2A;&#x5B57;&#x5178;&#xFF0C;&#x8D4B;&#x4E88;&#x4E00;&#x4E2A;&#x5B83;&#x4E13;&#x5C5E;&#x7684;&#x53D8;&#x91CF;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x6765;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Greeter2</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello</span></span>(%{<span class="hljs-symbol">name:</span> person_name} = person) <span class="hljs-keyword">do</span>
    IO.puts <span class="hljs-string">&quot;Hello, &quot;</span> &lt;&gt; person_name
    IO.inspect person
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x7531;&#x4E8E; Elixir &#x4F1A;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#x4F20;&#x5165;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x6240;&#x4EE5;&#xFF0C;&#x5728;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x4E24;&#x8FB9;&#x90FD;&#x4F1A;&#x5339;&#x914D;&#x4F20;&#x5165;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x6765;&#x7ED1;&#x5B9A;&#x4EFB;&#x4F55;&#x80FD;&#x5339;&#x914D;&#x4E0A;&#x7684;&#x90E8;&#x5206;&#x3002;&#x9996;&#x5148;&#xFF0C;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x770B;&#x53F3;&#x8FB9;&#x7684;&#x90E8;&#x5206;&#xFF1A;</p>
<pre><code class="lang-elixir">person = %{<span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Fred&quot;</span>, <span class="hljs-symbol">age:</span> <span class="hljs-string">&quot;95&quot;</span>, <span class="hljs-symbol">favorite_color:</span> <span class="hljs-string">&quot;Taupe&quot;</span>}
</code></pre>
<p>&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;<code>person</code> &#x88AB;&#x7ED1;&#x5B9A;&#x5E76;&#x8D4B;&#x503C;&#x4E3A;&#x6574;&#x4E2A; fred &#x5B57;&#x5178;&#x4E0A;&#x3002;&#x800C;&#x4E0B;&#x4E00;&#x4E2A;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#x5C31;&#x4F1A;&#x662F;&#x8FD9;&#x6837;&#xFF1A;</p>
<pre><code class="lang-elixir">%{<span class="hljs-symbol">name:</span> person_name} = %{<span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Fred&quot;</span>, <span class="hljs-symbol">age:</span> <span class="hljs-string">&quot;95&quot;</span>, <span class="hljs-symbol">favorite_color:</span> <span class="hljs-string">&quot;Taupe&quot;</span>}
</code></pre>
<p>&#x8FD9;&#x5C31;&#x548C;&#x6700;&#x5F00;&#x59CB;&#x7684; <code>Greeter1</code> &#x51FD;&#x6570;&#xFF0C;&#x4F20;&#x5165;&#x6574;&#x4E2A;&#x5B57;&#x5178;&#xFF0C;&#x4F46;&#x662F;&#x53EA;&#x5339;&#x914D;&#x4FDD;&#x7559; Fred &#x7684;&#x540D;&#x5B57;&#x60C5;&#x51B5;&#x662F;&#x4E00;&#x6837;&#x7684;&#x3002;&#x8FD9;&#x6837;&#x6211;&#x4EEC;&#x5C31;&#x5B9A;&#x4E49;&#x4E86;&#x4E24;&#x4E2A;&#x53D8;&#x91CF;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x4E86;&#xFF1A;</p>
<ol>
<li><code>person</code>&#xFF0C;&#x6307;&#x5411; <code>%{name: &quot;Fred&quot;, age: &quot;95&quot;, favorite_color: &quot;Taupe&quot;}</code></li>
<li><code>person_name</code>&#xFF0C;&#x6307;&#x5411; <code>&quot;Fred&quot;</code></li>
</ol>
<p>&#x6240;&#x4EE5;&#xFF0C;&#x5F53;&#x6211;&#x4EEC;&#x8C03;&#x7528; <code>Greeter2.hello/1</code> &#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E0A; Fred &#x7684;&#x6240;&#x6709;&#x6570;&#x636E;&#x4E86;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-comment"># &#x4F20;&#x5165;&#x6574;&#x4E2A;&#x4EBA;&#x7684;&#x8D44;&#x6599;</span>
...&gt; Greeter2.hello(fred)
<span class="hljs-string">&quot;Hello, Fred&quot;</span>
%{<span class="hljs-symbol">age:</span> <span class="hljs-string">&quot;95&quot;</span>, <span class="hljs-symbol">favorite_color:</span> <span class="hljs-string">&quot;Taupe&quot;</span>, <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Fred&quot;</span>}

<span class="hljs-comment"># &#x4F20;&#x5165;&#x53EA;&#x662F;&#x5305;&#x542B;&#x4E86; name &#x952E;&#x503C;&#x7684;&#x5B57;&#x5178;</span>
...&gt; Greeter4.hello(%{<span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Fred&quot;</span>})
<span class="hljs-string">&quot;Hello, Fred&quot;</span>
%{<span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Fred&quot;</span>}

<span class="hljs-comment"># &#x4F20;&#x5165;&#x4E0D;&#x5305;&#x542B; name &#x952E;&#x503C;&#x7684;&#x5B57;&#x5178;</span>
...&gt; Greeter4.hello(%{<span class="hljs-symbol">age:</span> <span class="hljs-string">&quot;95&quot;</span>, <span class="hljs-symbol">favorite_color:</span> <span class="hljs-string">&quot;Taupe&quot;</span>})
** (FunctionClauseError) no function clause matching <span class="hljs-keyword">in</span> Greeter2.hello/<span class="hljs-number">1</span>    

    The following arguments were given to Greeter2.hello/<span class="hljs-number">1</span><span class="hljs-symbol">:</span>

        <span class="hljs-comment"># 1</span>
        %{<span class="hljs-symbol">age:</span> <span class="hljs-string">&quot;95&quot;</span>, <span class="hljs-symbol">favorite_color:</span> <span class="hljs-string">&quot;Taupe&quot;</span>}

    <span class="hljs-symbol">iex:</span><span class="hljs-number">15</span><span class="hljs-symbol">:</span> Greeter2.hello/<span class="hljs-number">1</span>
</code></pre>
<p>&#x6240;&#x4EE5;&#xFF0C;Elixir &#x7684;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#x80FD;&#x6309;&#x4F20;&#x5165;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x72EC;&#x7ACB;&#x5339;&#x914D;&#x6BCF;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x3002;</p>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x6539;&#x53D8; <code>%{name: person_name}</code> &#x548C; <code>person</code> &#x7684;&#x987A;&#x5E8F;&#xFF0C;&#x7ED3;&#x679C;&#x8FD8;&#x662F;&#x4E00;&#x6837;&#x7684;&#x3002;&#x6BD4;&#x5982;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Greeter3</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello</span></span>(person = %{<span class="hljs-symbol">name:</span> person_name}) <span class="hljs-keyword">do</span>
    IO.puts <span class="hljs-string">&quot;Hello, &quot;</span> &lt;&gt; person_name
    IO.inspect person
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x7136;&#x540E;&#x548C;&#x8C03;&#x7528; <code>Greeter2.hello/1</code> &#x4E00;&#x6837;&#x4F20;&#x5165;&#x540C;&#x6837;&#x7684;&#x6570;&#x636E;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-comment"># &#x8FD8;&#x662F;&#x4F20;&#x5165; fred</span>
...&gt; Greeter3.hello(fred)
<span class="hljs-string">&quot;Hello, Fred&quot;</span>
%{<span class="hljs-symbol">age:</span> <span class="hljs-string">&quot;95&quot;</span>, <span class="hljs-symbol">favorite_color:</span> <span class="hljs-string">&quot;Taupe&quot;</span>, <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Fred&quot;</span>}
</code></pre>
<p>&#x8BB0;&#x4F4F;&#xFF0C;&#x867D;&#x7136;&#x770B;&#x8D77;&#x6765; <code>%{name: person_name} = person}</code> &#x8FD9;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x662F;&#x628A; <code>%{name: person_name}</code> &#x6A21;&#x5F0F;&#x5339;&#x914D;&#x5230; <code>person</code> &#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#x4E0A;&#xFF0C;&#x5176;&#x5B9E;&#x662F;&#x5B83;&#x4EEC; <em>&#x5404;&#x81EA;</em> &#x5339;&#x914D;&#x5230;&#x4F20;&#x5165;&#x7684;&#x53C2;&#x6570;&#x4E0A;&#x3002;</p>
<p><strong>&#x603B;&#x7ED3;:</strong> &#x51FD;&#x6570;&#x6309;&#x4F20;&#x5165;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x5404;&#x81EA;&#x72EC;&#x7ACB;&#x5339;&#x914D;&#x76F8;&#x5E94;&#x7684;&#x53C2;&#x6570;&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5728;&#x51FD;&#x6570;&#x5185;&#x7ED1;&#x5B9A;&#x591A;&#x4E2A;&#x72EC;&#x7ACB;&#x7684;&#x53D8;&#x91CF;&#x3002;</p>
<h3 id="&#x79C1;&#x6709;&#x51FD;&#x6570;">&#x79C1;&#x6709;&#x51FD;&#x6570;</h3>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x4E0D;&#x60F3;&#x5176;&#x4ED6;&#x6A21;&#x5757;&#x4F7F;&#x7528;&#x67D0;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x79C1;&#x6709;&#x51FD;&#x6570;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x53EA;&#x80FD;&#x88AB;&#x5B83;&#x6240;&#x5728;&#x6A21;&#x5757;&#x8C03;&#x7528;&#x7684;&#x51FD;&#x6570;&#x3002;&#x5728; Elixir &#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7528; <code>defp</code> &#x6765;&#x5B9A;&#x4E49;&#x79C1;&#x6709;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Greeter</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello</span></span>(name), <span class="hljs-symbol">do:</span> phrase &lt;&gt; name
  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">phrase</span></span>, <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;Hello, &quot;</span>
<span class="hljs-keyword">end</span>

iex&gt; Greeter.hello(<span class="hljs-string">&quot;Sean&quot;</span>)
<span class="hljs-string">&quot;Hello, Sean&quot;</span>

iex&gt; Greeter.phrase
** (UndefinedFunctionError) function Greeter.phrase/<span class="hljs-number">0</span> is undefined <span class="hljs-keyword">or</span> private
    Greeter.phrase()
</code></pre>
<h3 id="&#x54E8;&#x5175;&#x5B50;&#x53E5;&#xFF08;guard&#xFF09;">&#x54E8;&#x5175;&#x5B50;&#x53E5;&#xFF08;Guard&#xFF09;</h3>
<p>&#x6211;&#x4EEC;&#x5728;<a href="../control-structures">&#x63A7;&#x5236;&#x8BED;&#x53E5;</a>&#x90A3;&#x4E00;&#x4E2A;&#x63D0;&#x8FC7;&#x54E8;&#x5175;&#x5B50;&#x53E5;&#xFF0C;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x5C31;&#x6765;&#x770B;&#x770B;&#x600E;&#x4E48;&#x5728;&#x547D;&#x540D;&#x51FD;&#x6570;&#x4E2D;&#x4F7F;&#x7528;&#x5B83;&#x4EEC;&#x3002;&#x5F53; Elixir &#x5339;&#x914D;&#x67D0;&#x4E2A;&#x51FD;&#x6570;&#x4E4B;&#x540E;&#xFF0C;&#x540E;&#x9762;&#x7684;&#x54E8;&#x5175;&#x5B50;&#x53E5;&#x90FD;&#x4F1A;&#x88AB;&#x68C0;&#x6D4B;&#x3002;</p>
<p>&#x5728;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x4E86;&#x4E24;&#x4E2A;&#x6709;&#x76F8;&#x540C;&#x7B7E;&#x540D;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x800C;&#x4F9D;&#x8D56;&#x5224;&#x65AD;&#x53C2;&#x6570;&#x7C7B;&#x578B;&#x7684;&#x54E8;&#x5175;&#x5B50;&#x53E5;&#x6765;&#x786E;&#x5B9A;&#x8C03;&#x7528;&#x54EA;&#x4E2A;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Greeter</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello</span></span>(names) <span class="hljs-keyword">when</span> is_list(names) <span class="hljs-keyword">do</span>
    names
    |&gt; Enum.join(<span class="hljs-string">&quot;, &quot;</span>)
    |&gt; hello
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello</span></span>(name) <span class="hljs-keyword">when</span> is_binary(name) <span class="hljs-keyword">do</span>
    phrase() &lt;&gt; name
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">phrase</span></span>, <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;Hello, &quot;</span>
<span class="hljs-keyword">end</span>

iex&gt; Greeter.hello [<span class="hljs-string">&quot;Sean&quot;</span>, <span class="hljs-string">&quot;Steve&quot;</span>]
<span class="hljs-string">&quot;Hello, Sean, Steve&quot;</span>
</code></pre>
<h3 id="&#x9ED8;&#x8BA4;&#x53C2;&#x6570;">&#x9ED8;&#x8BA4;&#x53C2;&#x6570;</h3>
<p>&#x5982;&#x679C;&#x60F3;&#x7ED9;&#x53C2;&#x6570;&#x8BBE;&#x7F6E;&#x9ED8;&#x8BA4;&#x503C;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7528; <code>argument \\ value</code> &#x8BED;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Greeter</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello</span></span>(name, language_code \\ <span class="hljs-string">&quot;en&quot;</span>) <span class="hljs-keyword">do</span>
    phrase(language_code) &lt;&gt; name
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">phrase</span></span>(<span class="hljs-string">&quot;en&quot;</span>), <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;Hello, &quot;</span>
  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">phrase</span></span>(<span class="hljs-string">&quot;es&quot;</span>), <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;Hola, &quot;</span>
<span class="hljs-keyword">end</span>

iex&gt; Greeter.hello(<span class="hljs-string">&quot;Sean&quot;</span>, <span class="hljs-string">&quot;en&quot;</span>)
<span class="hljs-string">&quot;Hello, Sean&quot;</span>

iex&gt; Greeter.hello(<span class="hljs-string">&quot;Sean&quot;</span>)
<span class="hljs-string">&quot;Hello, Sean&quot;</span>

iex&gt; Greeter.hello(<span class="hljs-string">&quot;Sean&quot;</span>, <span class="hljs-string">&quot;es&quot;</span>)
<span class="hljs-string">&quot;Hola, Sean&quot;</span>
</code></pre>
<p>&#x5F53;&#x6211;&#x4EEC;&#x540C;&#x65F6;&#x4F7F;&#x7528;&#x536B;&#x5175;&#x548C;&#x9ED8;&#x8BA4;&#x53C2;&#x6570;&#x503C;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F1A;&#x9047;&#x5230;&#x95EE;&#x9898;&#xFF0C;&#x5148;&#x770B;&#x4E00;&#x4E0B;&#x7A0B;&#x5E8F;&#x4F1A;&#x62A5;&#x4EC0;&#x4E48;&#x9519;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Greeter</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello</span></span>(names, language_code \\ <span class="hljs-string">&quot;en&quot;</span>) <span class="hljs-keyword">when</span> is_list(names) <span class="hljs-keyword">do</span>
    names
    |&gt; Enum.join(<span class="hljs-string">&quot;, &quot;</span>)
    |&gt; hello(language_code)
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello</span></span>(name, language_code \\ <span class="hljs-string">&quot;en&quot;</span>) <span class="hljs-keyword">when</span> is_binary(name) <span class="hljs-keyword">do</span>
    phrase(language_code) &lt;&gt; name
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">phrase</span></span>(<span class="hljs-string">&quot;en&quot;</span>), <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;Hello, &quot;</span>
  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">phrase</span></span>(<span class="hljs-string">&quot;es&quot;</span>), <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;Hola, &quot;</span>
<span class="hljs-keyword">end</span>

** (CompileError) <span class="hljs-symbol">iex:</span><span class="hljs-number">31</span><span class="hljs-symbol">:</span> definitions with multiple clauses <span class="hljs-keyword">and</span> default values require a header. Instead <span class="hljs-symbol">of:</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">foo</span></span>(<span class="hljs-symbol">:first_clause</span>, b \\ <span class="hljs-symbol">:default</span>) <span class="hljs-keyword">do</span> ... <span class="hljs-keyword">end</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">foo</span></span>(<span class="hljs-symbol">:second_clause</span>, b) <span class="hljs-keyword">do</span> ... <span class="hljs-keyword">end</span>

one should <span class="hljs-symbol">write:</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">foo</span></span>(a, b \\ <span class="hljs-symbol">:default</span>)
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">foo</span></span>(<span class="hljs-symbol">:first_clause</span>, b) <span class="hljs-keyword">do</span> ... <span class="hljs-keyword">end</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">foo</span></span>(<span class="hljs-symbol">:second_clause</span>, b) <span class="hljs-keyword">do</span> ... <span class="hljs-keyword">end</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello</span></span>/<span class="hljs-number">2</span> has multiple clauses <span class="hljs-keyword">and</span> defines defaults <span class="hljs-keyword">in</span> one <span class="hljs-keyword">or</span> more clauses
    <span class="hljs-symbol">iex:</span><span class="hljs-number">31</span><span class="hljs-symbol">:</span> (<span class="hljs-keyword">module</span>)
</code></pre>
<p>Elixir &#x5728;&#x5904;&#x7406;&#x591A;&#x4E2A;&#x5339;&#x914D;&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4E0D;&#x559C;&#x6B22;&#x9ED8;&#x8BA4;&#x53C2;&#x6570;&#x8FD9;&#x79CD;&#x6A21;&#x5F0F;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x5F88;&#x5BB9;&#x6613;&#x8BA9;&#x4EBA;&#x6DF7;&#x6DC6;&#x3002;&#x8981;&#x5904;&#x7406;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x8BBE;&#x7F6E;&#x4E86;&#x9ED8;&#x8BA4;&#x53C2;&#x6570;&#x503C;&#x7684;&#x51FD;&#x6570;&#x5934;&#x90E8;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Greeter</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello</span></span>(names, language_code \\ <span class="hljs-string">&quot;en&quot;</span>)

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello</span></span>(names, language_code) <span class="hljs-keyword">when</span> is_list(names) <span class="hljs-keyword">do</span>
    names
    |&gt; Enum.join(<span class="hljs-string">&quot;, &quot;</span>)
    |&gt; hello(language_code)
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello</span></span>(name, language_code) <span class="hljs-keyword">when</span> is_binary(name) <span class="hljs-keyword">do</span>
    phrase(language_code) &lt;&gt; name
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">phrase</span></span>(<span class="hljs-string">&quot;en&quot;</span>), <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;Hello, &quot;</span>
  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">phrase</span></span>(<span class="hljs-string">&quot;es&quot;</span>), <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;Hola, &quot;</span>
<span class="hljs-keyword">end</span>

iex&gt; Greeter.hello [<span class="hljs-string">&quot;Sean&quot;</span>, <span class="hljs-string">&quot;Steve&quot;</span>]
<span class="hljs-string">&quot;Hello, Sean, Steve&quot;</span>

iex&gt; Greeter.hello [<span class="hljs-string">&quot;Sean&quot;</span>, <span class="hljs-string">&quot;Steve&quot;</span>], <span class="hljs-string">&quot;es&quot;</span>
<span class="hljs-string">&quot;Hola, Sean, Steve&quot;</span>
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="control-structures.html" class="navigation navigation-prev " aria-label="Previous page: 控制语句">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="pipe-operator.html" class="navigation navigation-next " aria-label="Next page: 管道操作符">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"1.2.0","title":"函数","level":"1.7","depth":1,"next":{"title":"管道操作符","level":"1.8","depth":1,"path":"lessons/basics/pipe-operator.md","ref":"lessons/basics/pipe-operator.md","articles":[]},"previous":{"title":"控制语句","level":"1.6","depth":1,"path":"lessons/basics/control-structures.md","ref":"lessons/basics/control-structures.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/basics/functions.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

